using System;
using System.Runtime.Serialization;
using System.Security.Permissions;

namespace Microsoft.WowAddonStudio.FrameXml.Editor.ToolWindows
{
	/// <summary>
	/// Represents errors that occur during LuaRuntime execution.
	/// </summary>
	[Serializable]
	public class ControllerInitializationException : Exception
	{
		/// <summary>
		/// Gets the name of the service which caused the exception.
		/// </summary>
		public string ServiceName { get; protected set; }

		/// <summary>
		/// Initializes a new instance of the <see cref="ControllerInitializationException"/> class.
		/// </summary>
		public ControllerInitializationException()
			: this(null, null, null)
		{
			
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ControllerInitializationException"/> class.
		/// </summary>
		/// <param name="message">The message.</param>
		/// <param name="exception">The exception.</param>
		public ControllerInitializationException(string message, Exception exception)
			: this(null, message, exception)
		{
			
		}
		/// <summary>
		/// Initializes a new instance of the <see cref="ControllerInitializationException"/> class.
		/// </summary>
		/// <param name="serviceName">The serviceName parameter.</param>
		public ControllerInitializationException(string serviceName)
			: this(serviceName, null, null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ControllerInitializationException"/> class
		///  with a specified error message.
		/// </summary>
		/// <param name="serviceName">The serviceName parameter.</param>
		/// <param name="message">The message that describes the error.</param>
		public ControllerInitializationException(string serviceName, string message)
			: this(serviceName, message, null)
		{
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ControllerInitializationException"/> class with a specified error message
		/// and a reference to the inner exception that is the cause of this exception.
		/// </summary>
		/// <param name="serviceName">The serviceName parameter.</param>
		/// <param name="message">The message that describes the error.</param>
		/// <param name="innerException">The exception that is the cause of the current exception, or a null reference if no inner exception is specified.</param>
		public ControllerInitializationException(string serviceName, string message, Exception innerException)
			: base(message, innerException)
		{
			ServiceName = serviceName;
		}

		/// <summary>
		/// Initializes a new instance of the <see cref="ControllerInitializationException"/> class with serialized data.
		/// </summary>
		/// <param name="info">The <see cref="System.Runtime.Serialization.SerializationInfo"/> that holds the serialized object data about the exception being thrown.</param>
		/// <param name="context">The <see cref="System.Runtime.Serialization.StreamingContext"/> that contains contextual information about the source or destination.</param>
		protected ControllerInitializationException(SerializationInfo info, StreamingContext context)
			: base(info, context)
		{
			if (info != null)
				ServiceName = info.GetString("ServiceName");
		}

		/// <summary>
		/// When overridden in a derived class, sets the System.Runtime.Serialization.SerializationInfo with information about the exception.
		/// </summary>
		/// <param name="info"></param>
		/// <param name="context"></param>
		[SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
		public override void GetObjectData(SerializationInfo info, StreamingContext context)
		{
			base.GetObjectData(info, context);
			info.AddValue("ServiceName", ServiceName);
		}

		/// <summary>
		/// Gets a message that describes the current exception.
		/// </summary>
		/// <returns>The error message that explains the reason for the exception, or an empty string("").</returns>
		public override string Message
		{
			get
			{
				string message = base.Message;
				if (InnerException != null)
					message = String.Concat(message, "\n", InnerException.Message);
				return message;
			}
		}
	}
}